package mya_dc.compilation_server.gui;

import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.TitledBorder;

import mya_dc.compilation_server.CompilationServer;
import mya_dc.compilation_server.database.DatabaseManager;
import mya_dc.compilation_server.threads.MainServerConnectionThread;

public class ServerPropertiesWindows extends JDialog 
{
	private JSpinner ReconnectionTimeSpinner;
	private static final long serialVersionUID = 9213523552154913411L;
	private JSpinner FreeSpaceSpinner;
	private JTextField usersNumField;
	private JTextField projectNumField;
	private JTextField directoryField;
	private JTextField IDField;
	private static final int MinFreeSpacePossible = 10485760;
	private static final int MinReconnectionTimePossible = 5000;
	
	public ServerPropertiesWindows(CompilationServer Server) {
		super();
		getContentPane().setLayout(null);
		setModalityType(ModalityType.DOCUMENT_MODAL);
		setModal(true);
		setResizable(false);
		setTitle("Compilation Server Properties");
		setBounds(200, 200, 567, 349);

		final JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBorder(new TitledBorder(null, "Compilation Server Properties", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, SystemColor.textHighlight));
		panel.setBounds(10, 10, 541, 262);
		getContentPane().add(panel);

		final JLabel registratedUsersLabel = new JLabel();
		registratedUsersLabel.setBounds(30, 67, 120, 30);
		panel.add(registratedUsersLabel);
		registratedUsersLabel.setText("Regisrated Users:");

		usersNumField = new JTextField();
		usersNumField.setBounds(248, 67, 105, 30);
		usersNumField.setText(Integer.toString(DatabaseManager.countUsers()));
		usersNumField.setHorizontalAlignment(JTextField.CENTER);
		panel.add(usersNumField);
		usersNumField.setEditable(false);

		final JLabel IDLabel = new JLabel();
		IDLabel.setBounds(30, 31, 120, 30);
		panel.add(IDLabel);
		IDLabel.setText("ID:");

		IDField = new JTextField();
		IDField.setBounds(248, 31, 105, 30);
		Long ID = Server.getServerID();
		if (ID != -1)
		{
			IDField.setText(ID.toString());
		}
		else
		{
			IDField.setText("");
		}
		IDField.setHorizontalAlignment(JTextField.CENTER);
		IDField.setEditable(false);
		panel.add(IDField);

		final JLabel rootDirectoryLabel = new JLabel();
		rootDirectoryLabel.setBounds(30, 210, 105, 30);
		panel.add(rootDirectoryLabel);
		rootDirectoryLabel.setText("Root Directory:");

		directoryField = new JTextField();
		directoryField.setBounds(125, 210, 390, 30);
		directoryField.setText(Server.getServerRoot());
		panel.add(directoryField);
		directoryField.setEditable(false);
		directoryField.setHorizontalAlignment(JTextField.CENTER);

		final JLabel registratedProjectsLabel = new JLabel();
		registratedProjectsLabel.setBounds(30, 103, 145, 30);
		panel.add(registratedProjectsLabel);
		registratedProjectsLabel.setText("Registrated Projects:");

		final JLabel minimumFreeSpaceLabel = new JLabel();
		minimumFreeSpaceLabel.setBounds(30, 140, 189, 30);
		panel.add(minimumFreeSpaceLabel);
		minimumFreeSpaceLabel.setText("Minimum Free Space (Bytes)");

		projectNumField = new JTextField();
		projectNumField.setBounds(248, 103, 105, 30);
		projectNumField.setText(Integer.toString(DatabaseManager.countProjects()));
		projectNumField.setHorizontalAlignment(JTextField.CENTER);
		panel.add(projectNumField);
		projectNumField.setEditable(false);
		
		final JLabel minimumFreeSpaceLabel_1 = new JLabel();
		minimumFreeSpaceLabel_1.setText("Reconnection Interval (Milliseconds):");
		minimumFreeSpaceLabel_1.setBounds(30, 175, 208, 30);
		panel.add(minimumFreeSpaceLabel_1);
		
		SpinnerNumberModel Model = new SpinnerNumberModel(CompilationServer.getFreeSpaceMinimum(),MinFreeSpacePossible,Long.MAX_VALUE,1);
		FreeSpaceSpinner = new JSpinner(Model);
		FreeSpaceSpinner.setBounds(248, 145, 105, 20);
		panel.add(FreeSpaceSpinner);

		SpinnerNumberModel NumModel = new SpinnerNumberModel(MainServerConnectionThread.getSleepTime(),MinReconnectionTimePossible,Integer.MAX_VALUE,100);
		ReconnectionTimeSpinner = new JSpinner(NumModel);
		ReconnectionTimeSpinner.setBounds(248, 180, 105, 20);
		panel.add(ReconnectionTimeSpinner);
		
		final JButton okButton = new JButton();
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				Double CurrValue = (Double)(FreeSpaceSpinner.getModel().getValue());
				CompilationServer.setFreeSpaceMinimum(CurrValue.longValue());
				MainServerConnectionThread.setSleepTime((Integer)ReconnectionTimeSpinner.getModel().getValue());
				setVisible(false);
			}
		});
		
		okButton.setText("OK");
		okButton.setBounds(225, 280, 107, 24);
		getContentPane().add(okButton);

		setVisible(true);
	}
}
